﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.InteropServices.ComTypes;
using System.Text;

namespace Auto.Tools.Standards.Extends
{
    /// <summary>
    /// Lambda Expression 合并扩展类
    /// </summary>
    public static class ExpressionWhereExtend
    {
        /// <summary>
        /// 扩展 Expression AND 运算（单实体）
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="original">初始的Expression</param>
        /// <param name="objectives">添加的目标Expression</param>
        /// <returns></returns>
        public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> original, Expression<Func<T, bool>> objectives)
        {
            return original = original.Compose(objectives, Expression.AndAlso);
        }
        /// <summary>
        /// 扩展 Expression AND 运算（多实体）
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="original"></param>
        /// <param name="objectives"></param>
        /// <returns></returns>
        public static Expression<Func<T1, T2, bool>> And<T1, T2>(this Expression<Func<T1, T2, bool>> original, Expression<Func<T1, T2, bool>> objectives)
        {
            return original = original.Composes(objectives, Expression.And);
        }


        /// <summary>
        ///（扩展）合并一个Expression OR 运算
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="original">初始的Expression</param>
        /// <param name="objectives">添加的目标Expression</param>
        /// <returns></returns>
        public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> original, Expression<Func<T, bool>> objectives)
        {
            return original = original.Compose(objectives, Expression.OrElse);
        }


        /// <summary>
        ///（扩展）合并一个Expression OR 运算（多实体）
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="original">初始的Expression</param>
        /// <param name="objectives">添加的目标Expression</param>
        /// <returns></returns>
        public static Expression<Func<T1, T2, bool>> Or<T1, T2>(this Expression<Func<T1, T2, bool>> original, Expression<Func<T1, T2, bool>> objectives)
        {
            return original = original.Composes(objectives, Expression.OrElse);
        }

        /// <summary>
        /// 合并两个lamdba Expression
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="original">第一个lamdba Expression</param>
        /// <param name="objectives">第二个lamdba Expression</param>
        /// <param name="mergeMethod">合并方法</param>
        /// <returns></returns>
        public static Expression<Func<T, bool>> Compose<T>(this Expression<Func<T, bool>> original, Expression<Func<T, bool>> objectives, Func<Expression, Expression, Expression> mergeMethod)
        {
            var invokedExp = Expression.Invoke(objectives, original.Parameters.Cast<Expression>());
            return Expression.Lambda<Func<T, bool>>(mergeMethod(original.Body, invokedExp), original.Parameters);
        }

        /// <summary>
        /// 合并两个lamdba Expression
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="original">第一个lamdba Expression</param>
        /// <param name="objectives">第二个lamdba Expression</param>
        /// <param name="mergeMethod">合并方法</param>
        /// <returns></returns>
        public static Expression<T> Composes<T>(this Expression<T> original, Expression<T> objectives, Func<Expression, Expression, Expression> mergeMethod)
        {
            var invokedExp = Expression.Invoke(objectives, original.Parameters.Cast<Expression>());
            return Expression.Lambda<T>(mergeMethod(original.Body, invokedExp), original.Parameters);
        }


    }

    /// <summary>
    /// 
    /// </summary>
    public class ParameterVisitor : ExpressionVisitor
    {
        private readonly Dictionary<ParameterExpression, ParameterExpression> map;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="map"></param>
        public ParameterVisitor(Dictionary<ParameterExpression, ParameterExpression> map)
        {
            this.map = map ?? new Dictionary<ParameterExpression, ParameterExpression>();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="map"></param>
        /// <param name="exp"></param>
        /// <returns></returns>
        public static Expression ReplaceParameters(Dictionary<ParameterExpression, ParameterExpression> map, Expression exp)
        {
            return new ParameterVisitor(map).Visit(exp);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        protected override Expression VisitParameter(ParameterExpression p)
        {
            if (map.TryGetValue(p, out ParameterExpression replacement))
            {
                p = replacement;
            }
            return base.VisitParameter(p);
        }
    }


}
